<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:gml="http://www.opengis.net/gml" 
	xmlns:ogr="http://ogr.maptools.org/"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:nuts="http://nuts.geovocab.org/id/"
	xmlns:ramon="http://rdfdata.eionet.europa.eu/ramon/ontology/"
	xmlns:ramon_geo="http://ec.europa.eu/eurostat/ramon/ontologies/geographic.rdf#" 
	xmlns:fao="http://aims.fao.org/aos/geopolitical.owl#"
	xmlns:ngeo="http://geovocab.org/geometry#"
	xmlns:spatial="http://geovocab.org/spatial#"
	xmlns:owl="http://www.w3.org/2002/07/owl#">
	
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ogr:FeatureCollection">

	<xsl:variable name="dbpedia_dump" select="document('../sources/dbpedia/nuts_dbpedia_settlements_over_10000.xml')"/>
	<xsl:variable name="nuts_dump" select="document('../sources/ramon/nuts2008.rdf')"/>
	<xsl:variable name="eionet_dump" select="document('../sources/eionet/eionet.rdf')"/>	
	<xsl:variable name="fao_dump" select="document('../sources/fao/geopolitical.owl')"/>	
	<xsl:variable name="ukos_mappings" select="document('../sources/ukos/ukos_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="geoes_mappings" select="document('../sources/geoes/geoes_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="touches_relations" select="document('../sources/touches.xml')"/>	

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >
		<xsl:variable name="nuts_id" select="ogr:NUTS_ID"/>
		<xsl:variable name="nuts_uri" select="concat('http://nuts.geovocab.org/id/',$nuts_id)"/>
		<xsl:variable name="eionet_uri" select="concat('http://rdfdata.eionet.europa.eu/eea/countries/',$nuts_id)"/>

		<xsl:result-document href="{concat('id/',ogr:NUTS_ID,'.rdf')}">
			<rdf:RDF>
			<xsl:apply-templates select="ogr:geometryProperty">
				<xsl:with-param name="dbpedia_nodes" select="$dbpedia_dump/table/row[column[position()=1]=$nuts_uri]" />
				<xsl:with-param name="nuts_node" select="$nuts_dump/rdf:RDF/ramon_geo:NUTSRegion[@rdf:about=$nuts_id]" />
				<xsl:with-param name="eionet_node" select="$eionet_dump/rdf:RDF/rdf:Description[@rdf:about=$eionet_uri]" />
				<xsl:with-param name="fao_node" select="$fao_dump/rdf:RDF/fao:self_governing[fao:codeISO2=$nuts_id]" />
				<xsl:with-param name="ukos_node" select="$ukos_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
				<xsl:with-param name="geoes_node" select="$geoes_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
				<xsl:with-param name="touches_nodes" select="$touches_relations/table/row[column[position()=1]=$nuts_uri]" />
				<xsl:with-param name="nuts_id" select="$nuts_id" />
			</xsl:apply-templates>
			</rdf:RDF>
		</xsl:result-document>

	</xsl:for-each>

</xsl:template>

<xsl:template name="geometry" match="ogr:geometryProperty">
	<xsl:param name="dbpedia_nodes" />
	<xsl:param name="nuts_node" />
	<xsl:param name="eionet_node" />
	<xsl:param name="fao_node" />
	<xsl:param name="ukos_node" />
	<xsl:param name="geoes_node" />
	<xsl:param name="touches_nodes" />
	<xsl:param name="nuts_id" />
	
	<xsl:variable name="geometry_id" select="concat('nuts:',$nuts_id,'_geometry')"/>
	
	<rdf:Description rdf:about="{concat('http://nuts.geovocab.org/id/',$nuts_id)}">
		<rdf:type rdf:resource="http://rdfdata.eionet.europa.eu/ramon/ontology/NUTSRegion"/>
		<rdf:type rdf:resource="http://geovocab.org/spatial#Feature"/>
		<ngeo:geometry rdf:resource="{concat('http://nuts.geovocab.org/id/',$nuts_id,'_geometry')}"/>
		<rdfs:label><xsl:value-of select="$nuts_node/rdfs:label" /></rdfs:label>
		<ramon:name><xsl:value-of select="$nuts_node/ramon_geo:name" /></ramon:name>
		<ramon:level rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"><xsl:value-of select="$nuts_node/ramon_geo:level" /></ramon:level>
		<ramon:code><xsl:value-of select="$nuts_node/ramon_geo:code" /></ramon:code>
		
		<xsl:if test="$nuts_node/ramon_geo:hasParentRegion">
			<spatial:PP rdf:resource="{concat('http://nuts.geovocab.org/id/',$nuts_node/ramon_geo:hasParentRegion/@rdf:resource)}"/>
		</xsl:if>

		<xsl:if test="$fao_node">
			<xsl:for-each select='$fao_node/fao:hasBorderWith' >
				<xsl:variable name="ec_node" select="substring-after(./@rdf:resource, '#')"/>
				<xsl:variable name="codeISO2" select="../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2"/>
				<xsl:if test="$nuts_node/../ramon_geo:NUTSRegion[@rdf:about=$codeISO2]">
					<spatial:EC rdf:resource="{concat('http://nuts.geovocab.org/id/',../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2)}" />
				</xsl:if>
			</xsl:for-each>
		</xsl:if>

		<xsl:if test="$dbpedia_nodes">
			<xsl:for-each select='$dbpedia_nodes' >
				<spatial:PPi rdf:resource="{column[position()=last()]}"/>
			</xsl:for-each>
		</xsl:if>

		<xsl:if test="$touches_nodes">
			<xsl:for-each select='$touches_nodes' >
				<spatial:EC rdf:resource="{column[position()=last()]}"/>
			</xsl:for-each>
		</xsl:if>
		
		<owl:sameAs rdf:resource="{concat('http://rdfdata.eionet.europa.eu/ramon/nuts2008/',$nuts_id)}"/>
		<owl:sameAs rdf:resource="{concat('http://ec.europa.eu/eurostat/ramon/rdfdata/nuts2008/',$nuts_id)}"/>
		<owl:sameAs rdf:resource="{concat('http://nuts.psi.enakting.org/id/',$nuts_id)}"/>
		<owl:sameAs rdf:resource="{concat('http://estatwrap.ontologycentral.com/dic/geo#',$nuts_id)}"/>
		<xsl:if test="$eionet_node">
			<xsl:for-each select='$eionet_node/owl:sameAs' >
				<owl:sameAs rdf:resource="{./@rdf:resource}"/>
			</xsl:for-each>
		</xsl:if>

		<xsl:if test="$ukos_node">
			<xsl:for-each select='$ukos_node/rdfs:seeAlso' >
				<rdfs:seeAlso rdf:resource="{./@rdf:resource}"/>
			</xsl:for-each>
		</xsl:if>

		<xsl:if test="$geoes_node">
			<xsl:for-each select='$geoes_node/rdfs:seeAlso' >
				<rdfs:seeAlso rdf:resource="{./@rdf:resource}"/>
			</xsl:for-each>
		</xsl:if>

    </rdf:Description>
</xsl:template>

</xsl:stylesheet>